package com.example.gw_back_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gw_back_end.domain.PageParam;
import com.example.gw_back_end.domain.PageResult;
import com.example.gw_back_end.domain.entities.PostComment;
import com.example.gw_back_end.domain.entities.User;
import com.example.gw_back_end.mapper.PostCommentMapper;
import com.example.gw_back_end.mapper.UserMapper;
import com.example.gw_back_end.service.PostCommentService;
import com.example.gw_back_end.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment> implements PostCommentService {

    private final PostCommentMapper postCommentMapper;
    private final UserMapper userMapper;
    public PostCommentServiceImpl(PostCommentMapper postCommentMapper,UserMapper userMapper) {
        this.postCommentMapper = postCommentMapper;
        this.userMapper = userMapper;
    }

    @Override
    public boolean addPostComment(PostComment postComment) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        postComment.setUserId(id);
        postComment.setCommentTime(LocalDateTime.now());
        return postCommentMapper.insert(postComment) > 0;
    }

    @Override
    public boolean deletePostComment(int id) {
        return postCommentMapper.deleteById(id) > 0;
    }

    @Override
    public PostComment getPostCommentById(int id) {
        return postCommentMapper.selectById(id);
    }

    @Override
    public boolean updatePostComment(PostComment postComment) {
        return postCommentMapper.updateById(postComment) > 0;
    }

    @Override
    public PageResult<PostComment> getPostCommentByPage(PageParam pageParam,Integer postId) {
        LambdaQueryWrapper<PostComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PostComment::getPostId, postId)
                .orderByDesc(PostComment::getCommentTime); // 按评论时间倒序

        Page<PostComment> postCommentPage = postCommentMapper.selectPage(
                new Page<>(pageParam.getPage(), pageParam.getSize()),
                lambdaQueryWrapper
        );

        // 2. 提取评论中的用户ID集合
        Set<Integer> userIds = postCommentPage.getRecords().stream()
                .map(PostComment::getUserId)
                .collect(Collectors.toSet());

        // 3. 批量查询用户信息
        if(!userIds.isEmpty()) {
            Map<Integer, User> userMap = userMapper.selectBatchIds(userIds)
                    .stream()
                    .collect(Collectors.toMap(User::getId, Function.identity()));

            // 4. 为每条评论设置用户信息
            postCommentPage.getRecords().forEach(comment -> {
                User user = userMap.get(comment.getUserId());
                if(user != null) {
                    comment.setUsername(user.getUsername());
                    comment.setAvatar(user.getAvatar());
                }
            });
        }

        return PageResult.setPageResult(postCommentPage);
    }
}
